<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of estatisticas
 *
 * @author root
 */
class Estatisticas {
    
    static function acesso_pagina($cod = 0,$tpo = 0){
        
        //$tabela = 'ACESSO_PAGINAS';
        
        $tabela = 'ACESSO_PAGINAS_'.date('Y');
        
        self::verifica_tabela_acessos($tabela);
        
        $front = Zend_Controller_Front::getInstance();
        $session = Zend_Auth::getInstance();
        
        $nao_reg = array('acessopaginas');
        
        if(array_search($front->getRequest()->getControllerName(), $nao_reg) === FALSE){
            $post = '';
            if($front->getRequest()->isPost()){
                foreach($_POST as $key => $value) 
                    if(is_array($value)){
                        foreach($value as $key2 => $value2) $post .= '&'.$key.'[]='.$value2; 
                    }
                    else $post .= '&'.$key.'='.$value; 
            }

            $data = array(
                'ACE_CTL'   => $front->getRequest()->getControllerName(),
                'ACE_ACT'   => $front->getRequest()->getActionName(),
                'ACE_COD'   => $cod,
                'USU_ID'    => $session->getStorage()->read()->USU_ID,
                'ACE_DAT'=> date('Y-m-d'),
                'ACE_HOR'=> date('H:i:s'),
                'ACE_ANO'=> date('Y'),
                'ACE_URL'   => $_SERVER ['REQUEST_URI'],
                'ACE_TPO'   => $tpo,
                'ACE_PST'   => $post,
                'ACE_IP'    => $_SERVER["REMOTE_ADDR"], 
            );
            
            $db = Zend_Db_Table_Abstract::getDefaultAdapter();
            $db->insert($tabela,$data);
        }
    }
    
    static function verifica_tabela_acessos($tabela){
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        try {
            $query = 'SHOW TABLES LIKE "'.$tabela.'"';//'SELECT * FROM teste WHERE 1';
            $result = $db->fetchRow($query);
            
            if(is_array($result)) return true;
            else self::cria_tabela($tabela);
            
        } catch (Exception $exc) {
            self::cria_tabela($tabela);
        }
        
    }
    
    static function cria_tabela($tabela){
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $query = "CREATE TABLE IF NOT EXISTS `".$tabela."` (
                    `ACE_ID` int(11) NOT NULL AUTO_INCREMENT,
                    `ACE_URL` varchar(250) COLLATE latin1_general_ci NOT NULL,
                    `ACE_CTL` varchar(40) COLLATE latin1_general_ci NOT NULL,
                    `ACE_ACT` varchar(40) COLLATE latin1_general_ci NOT NULL,
                    `ACE_COD` int(11) NOT NULL,
                    `USU_ID` int(11) NOT NULL,
                    `ACE_DAT` date NOT NULL,
                    `ACE_HOR` time NOT NULL,
                    `ACE_ANO` int(4) NOT NULL,
                    `ACE_TPO` int(1) NOT NULL COMMENT '1 = SISTEMA, 2 = EAD',
                    `ACE_PST` text COLLATE latin1_general_ci NOT NULL,
                    `ACE_IP` varchar(15) COLLATE latin1_general_ci NOT NULL,
                    PRIMARY KEY (`ACE_ID`),
                    KEY `USU_ID` (`USU_ID`),
                    KEY `ACE_TPO` (`ACE_TPO`),
                    KEY `ACE_ANO` (`ACE_ANO`),
                    KEY `ACE_DAT` (`ACE_DAT`)
                  ) ENGINE=InnoDB";
        
        $db->query($query);
    }
}

?>
